﻿using System;
using System.Collections;
using System.Data;
using System.Web.UI.WebControls;
using Model;
using Resources;
using Service;

public partial class EmployeeCompany : System.Web.UI.Page
{

    readonly ReferenceDataService _ReferenceDataService = new ReferenceDataService();
    readonly StockService _StockService = new StockService();
    private DirectService _DirectService = new DirectService();


    protected void Page_Load(object sender, EventArgs e)
    {
       // lblTotalCompany.Text = "Total company:" + _ReferenceDataService.GetCompanies().Count;

        //  txtHiddenSystemId.Text = Request.QueryString["systemsId"];
        if (Session["whereClause"] == null)
            Session["whereClause"] = txtHiddenWhereClause.Text;
        if (Session["orderByClause"] == null)
            Session["orderByClause"] = txthiddenOrderbyClause.Text;


        new Utility().SetRowToDisplay(grdCompany, Session["rowsToSDisplay"]);
        if (!IsPostBack)
        {
            // to avoid loading for the first time



            if (Session["pageIndex"] != null)
            {
                grdCompany.PageIndex = Int32.Parse(Session["pageIndex"].ToString());

            }

            /* 
             * 1. number of equipments
             * 2. select the element 
             * */
           
        }
        else
        {
            //  SystemList = new DirectService().DataTableToIlistSystem(GetWhereClause(), GetOrderClause());
            txtHiddenPageIndex.Text = "0";

            lblTotalCompany.Text = "Total company: " + new DirectService().GetEmployeeCompanyDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            int number = +new DirectService().GetEmployeeCompanyDisplayed(GetWhereClause(), GetOrderClause()).Rows.Count;
            if (number == 0)
            {
                grdCompany1.Visible = true;
            }
            else
            {
                grdCompany.Visible = true;
                grdCompany1.Visible = false;
            }
        }
        grdCompany.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;

    }
    protected void bntFilter_Click(object sender, EventArgs e)
    {

        Session["whereClause"] = GetWhereClause();
        Session["orderByClause"] = GetOrderClause();
        txthiddenOrderbyClause.Text = GetOrderClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;
        ClearControlSearch();
        ClearControlAdd();
    }
    private void ClearControlSearch()
    {
        txtCompanyId.Text = "";
        txtCompanyLabel.Text = ""; 
    }
    protected void grdDepartment_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdCompany.PageIndex = e.NewPageIndex;
        txtHiddenPageIndex.Text = grdCompany.PageIndex.ToString();
        Session["pageIndex"] = grdCompany.PageIndex;
    }
    protected string GetWhereClause()
    {
        string whereClause = string.Empty;
        string and = "";
        string str = "";

        // Prepare by equipement
        int i = 0;
        if (!string.IsNullOrEmpty(txtCompanyId.Text))
        {
            if (int.TryParse(txtCompanyId.Text, out i))
            {
                and = " and ";
                str = " where Company.CompanyId=" + Int32.Parse(txtCompanyId.Text);
                if (txtCompanyLabel.Text != string.Empty)
                {
                    str += " " + "and" + " " + "Company.CompanyName=" + "'" + txtCompanyLabel.Text + "'";
                    and = " and ";
                }
            }else
            {
                grdCompany.Visible = false;
                grdCompany1.Visible = true;
                lblTotalCompany.Text = "Total company: 0";
                return null;
            }
        }
        else
        {
            if (txtCompanyLabel.Text != string.Empty)
            {
                str += " " + "where" + " " + "Company.CompanyName like" + "'%" + txtCompanyLabel.Text + "%'";
                and = " and ";
            }
        }

        if (!String.IsNullOrEmpty(str))
            whereClause = str + " ";
        return whereClause;
    }
    protected string GetOrderClause()
    {
        return " Order by Company.CompanyName";
    }

    protected void grdDepartment_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName.Equals("Delete"))
        {
            int companyId = Convert.ToInt32(grdCompany.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
            _StockService.DeleteCompany(companyId);
            Response.Redirect("EmployeeCompany.aspx");
        }
        new Utility().SetSelectedColorToGridView(e, grdCompany);

    }
    protected void grdDepartment_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void grdDepartment_Sorted(object sender, EventArgs e)
    {

    }
    protected void grdDepartment_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //block to add message
            string stringComplete = "";
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                LinkButton deleteButton = (LinkButton)e.Row.Cells[1].Controls[0];
                deleteButton.Attributes.Add("onclick", "return confirm('" + Resource.ConfirmDeleteMessage + "?');");
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

        AddEmployeeCompany();
        
    }
    private void AddEmployeeCompany()
    {

        IList list = new ArrayList();
        list = _StockService.GetCompany();

        for (int i = 0; i < list.Count; i++)
        {
            if (((Company)list[i]).CompanyLabel == txtCompany.Text)
            {
                lblMessage.Visible = true;
                lblMessageValidate.Text = ""; 
                return;
            }
            lblMessage.Visible = false;

        }
        if (txtCompany.Text != string.Empty)
        {
            Company _company = new Company();
            _company.CompanyId = 0;
            _company.CompanyLabel = txtCompany.Text;
            _ReferenceDataService.SaveCompany(_company);
            lblTotalCompany.Text = "Total company:" + _ReferenceDataService.GetCompanies().Count;
            ClearControlAdd();
            lblMessageValidate.Text = Resource.MessageValidate; 
        }
       
    }
    private void ClearControlAdd()
    {

        txtCompany.Text = "";
        lblMessageValidate.Text = "";
        lblMessage.Text = ""; 

    }
    protected void dsCompany_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue != null) lblTotalCompany.Text = "Total company:" + ((DataTable)e.ReturnValue).Rows.Count;
    }
}
